package cxm.exam;

import java.util.List;
import java.util.concurrent.TimeUnit;

import cxm.exam.Api.NewsApi;
import cxm.exam.entity.HttpResult;
import cxm.exam.entity.HttpResult1;
import cxm.exam.entity.MyHttpResult;
import cxm.exam.entity.MyNewsEntity;
import cxm.exam.entity.MyNewsEntitys;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ThinkPad on 2017/5/12.
 */

public class HttpMethod {
//    //在访问HttpMethods时创建单例
//    private static class SingletonHolder{
//        private static final HttpMethod INSTANCE = new HttpMethod();
//    }
//
//    //获取单例
//    public static HttpMethod getInstance(){
//        return SingletonHolder.INSTANCE;
//    }
    private Retrofit retrofit;
    private NewsApi  mNewsApi;
    private static final int DEFAULT_TIMEOUT = 30;
    private static final String BASE_URL = "http://v.juhe.cn/";
    public HttpMethod()
    {
        // 创建 okhttpclient 并设置超时
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build();
        mNewsApi = retrofit.create(NewsApi.class);
    }
    // 获取新闻
    public  void getNews()
    {
        String strType = "top";
        String strKey = "e854c54e5c5648bec9f20c07a0c5909f";
       // Call<HttpResult<List<MyNewsEntity>>> listNewsCall = mNewsApi.getNews(strType,strKey);
      //  Call<HttpResult<MyNewsEntitys>> NewsCall = mNewsApi.getNews2(strType,strKey);
        Call<HttpResult1> result1Call = mNewsApi.getNews3(strType,strKey);
        result1Call.enqueue(new Callback<HttpResult1>() {
            @Override
            public void onResponse(Call<HttpResult1> call, Response<HttpResult1> response) {
                String header = response.headers().toString();
                String body = response.body().toString();
                HttpResult1 ret = response.body();
            }

            @Override
            public void onFailure(Call<HttpResult1> call, Throwable t) {
                String error = t.getMessage();
            }
        });

    }
    public void getNews1()
    {
        String strType = "top";
        String strKey = "e854c54e5c5648bec9f20c07a0c5909f";
        Observable<MyHttpResult> observable = mNewsApi.getNews4(strType,strKey);
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Subscriber<MyHttpResult>() {
                    @Override
                    public void onCompleted() {
                        String a = "onCompleted";
                    }

                    @Override
                    public void onError(Throwable e) {
                        String b = "onError";
                    }

                    @Override
                    public void onNext(MyHttpResult myHttpResult) {
                        MyHttpResult c = myHttpResult;
                    }
                });

    }

}
